"""
异步上下文管理器
此种对象通过定义 -aenter_() 和 -aexit() 方法来对 async with 语句中的环境进行控制。由 PEP 492 引入。
和异步迭代器一样，不能单独存在，需要放在协程函数内(async def)
"""
import asyncio

import pytest


class AsyncContextManager:
    def __init__(self):
        self.conn = None

    async def do_something(self):
        return 666

    async def __aenter__(self):
        self.conn = await asyncio.sleep(1)
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await asyncio.sleep(1)


@pytest.mark.asyncio
async def test_func():
    async with AsyncContextManager() as f:
        result = await f.do_something()
        print(result)



if __name__=="__main__":
    # asyncio.run(test_func())
    asyncio.run(asyncio.wait([test_func()]))